#include "kernel_operator.h"

constexpr int32_t TOTAL_LENGTH = 8 * 2048;                            
constexpr int32_t USE_CORE_NUM = 8;                                   
constexpr int32_t BLOCK_LENGTH = TOTAL_LENGTH / USE_CORE_NUM;         
constexpr int32_t TILE_NUM = 8;                                       
constexpr int32_t BUFFER_NUM = 2;                                     
constexpr int32_t TILE_LENGTH = BLOCK_LENGTH / TILE_NUM / BUFFER_NUM; 

class KernelL1Loss {
public:
    __aicore__ inline KernelL1Loss() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z,GM_ADDR s,uint32_t reduction)
    {
        this->reduction = static_cast<uint32_t>(reduction);
        sum=0.0;
        xGm.SetGlobalBuffer((__gm__ float *)x + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        yGm.SetGlobalBuffer((__gm__ float *)y + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        zGm.SetGlobalBuffer((__gm__ float *)z + BLOCK_LENGTH * AscendC::GetBlockIdx(), BLOCK_LENGTH);
        sGm.SetGlobalBuffer((__gm__ float *)s , TILE_LENGTH);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(inQueueY, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(outQueueS, BUFFER_NUM, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuf0, TILE_LENGTH * sizeof(float));
        pipe.InitBuffer(tmpBuf1, TILE_LENGTH * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = TILE_NUM * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.AllocTensor<float>();

        AscendC::DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
        AscendC::DataCopy(yLocal, yGm[progress * TILE_LENGTH], TILE_LENGTH);

        inQueueX.EnQue(xLocal);
        inQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> yLocal = inQueueY.DeQue<float>();
        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();
        AscendC::LocalTensor<float> sLocal = outQueueS.AllocTensor<float>();


        if(reduction == 0){//none
            AscendC::Sub(zLocal, xLocal, yLocal, TILE_LENGTH);
            AscendC::Abs(zLocal, zLocal, TILE_LENGTH);
        }else if(reduction == 1){//sum
            AscendC::Sub(zLocal, xLocal, yLocal, TILE_LENGTH);
            AscendC::Abs(zLocal, zLocal, TILE_LENGTH);
            // 进行归约
            for (int i = 0 ; i < TILE_LENGTH; i++) {
                float temp = zLocal.GetValue(i);
                sum = temp+sum;
            }
            if(progress == TILE_NUM * BUFFER_NUM - 1){
                sLocal.SetValue(0,sum);

            }
        }else if(reduction == 2) {//mean
            AscendC::Sub(zLocal, xLocal, yLocal, TILE_LENGTH);
            AscendC::Abs(zLocal, zLocal, TILE_LENGTH);
            // 进行归约
            for (int i = 0 ; i < TILE_LENGTH; i++) {
                float temp = zLocal.GetValue(i);
                sum = temp+sum;
            }
            if(progress == TILE_NUM * BUFFER_NUM - 1){
                sLocal.SetValue(0,sum/TOTAL_LENGTH);
            }
        }

        outQueueZ.EnQue<float>(zLocal);
        outQueueS.EnQue<float>(sLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueY.FreeTensor(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {   
        if(reduction==0){
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::LocalTensor<float> sLocal = outQueueS.DeQue<float>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        AscendC::DataCopy(sGm, sLocal, TILE_LENGTH);
        outQueueS.FreeTensor(sLocal);
        outQueueZ.FreeTensor(zLocal);
        }else if(reduction == 1){
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::LocalTensor<float> sLocal = outQueueS.DeQue<float>();
        AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
        if(progress == TILE_NUM * BUFFER_NUM - 1){
            AscendC::SetAtomicAdd<float>();
            AscendC::DataCopy(sGm, sLocal, TILE_LENGTH);
            AscendC::SetAtomicNone();
        }
            outQueueS.FreeTensor(sLocal);
            outQueueZ.FreeTensor(zLocal);
        }else if(reduction ==2){
            AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
            AscendC::LocalTensor<float> sLocal = outQueueS.DeQue<float>();
            AscendC::DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
            if(progress == TILE_NUM * BUFFER_NUM - 1){
                AscendC::SetAtomicAdd<float>();
                AscendC::DataCopy(sGm, sLocal, TILE_LENGTH);
                AscendC::SetAtomicNone();
            }
            outQueueS.FreeTensor(sLocal);
            outQueueZ.FreeTensor(zLocal);
        }
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX, inQueueY,inQueueS;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ,outQueueS;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf0;
    AscendC::TBuf<AscendC::TPosition::VECCALC> tmpBuf1;
    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> yGm;
    AscendC::GlobalTensor<float> zGm;
    AscendC::GlobalTensor<float> sGm;
    uint32_t reduction;
    float sum;
};

extern "C" __global__ __aicore__ void l1loss_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z,GM_ADDR s,uint32_t reduction)
{
    KernelL1Loss op;
    op.Init(x, y, z,s,reduction);
    op.Process();

}

#ifndef ASCENDC_CPU_DEBUG
void l1loss_custom_do(uint32_t blockDim, void *stream, uint8_t *x, uint8_t *y, uint8_t *z,uint8_t *s,uint32_t reduction)
{
    l1loss_custom<<<blockDim, nullptr, stream>>>(x, y, z,s,reduction);
}
#endif
